package com.framework.base.interceptor;

import java.util.Map;

import org.apache.log4j.Logger;

import com.framework.base.constant.WebConstants;
import com.framework.base.util.MemCacheUtil;
import com.framework.base.util.Struts2Utils;
import com.framework.main.sys.user.domain.UserVo;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.interceptor.AbstractInterceptor;

/**
 * 用于判断操作权限
 * 
 * @author www.51wuxia.com
 * 
 */
public class LoginAbstractInterceptor extends AbstractInterceptor {

	private static final long serialVersionUID = 9170936534167917367L;
	private static final String LOGIN = "Login";// 不拦截登录操作
	private static final String TOLOGIN = "toLogin";
	Logger logger = Logger.getLogger(getClass()); // 作为日志的公用对象

	@Override
	public String intercept(ActionInvocation invocation) throws Exception {
		Map<?, ?> servletContext=invocation.getInvocationContext().getApplication();
		Map<?, ?> session = invocation.getInvocationContext().getSession();
		//登录和首页跳转不拦截
		if (LOGIN.equalsIgnoreCase(invocation.getProxy().getConfig().getName())) {
			return invocation.invoke();
		}
		if (TOLOGIN.equalsIgnoreCase(invocation.getProxy().getConfig()
				.getName())) {
			return invocation.invoke();
		}
		UserVo user = (UserVo) session.get(WebConstants.CURRENTUSER);
		if (user == null) {
			return Action.INPUT;
		}
		//判断操作权限
		String action=invocation.getProxy().getConfig().getName();
		String power;
		if(WebConstants.HASMEMCACHE){
			power=(String)MemCacheUtil.readObject(Struts2Utils.getCurrentUser().getIdf());
		}else{
			power=(String)session.get(WebConstants.USERPERMISSIONS);//memcache缓存不存在权限，就去session中取值
		}
		if(power==null){
			WebConstants.HASMEMCACHE=false;
			return Action.INPUT;
		}
		return invocation.invoke();
	}

}
